package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameBag.*;
import com.motu.monstercity.server.game.base.*;
import com.motu.monstercity.server.game.commondata.act.ActTopRanking;
import com.motu.monstercity.server.game.commondata.bag.*;
import com.motu.monstercity.server.game.commondata.build.BuildConstant;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.commondata.bag.ChipMain;
import com.motu.monstercity.server.game.commondata.partner.PartnerMain;
import com.motu.monstercity.server.game.commondata.partner.WeaponMain;
import com.motu.monstercity.server.game.commondata.secretary.SecretaryMain;
import com.motu.monstercity.server.game.commondata.system.SystemConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BagHandler {
	// CMD_GAME_BAG_ITEM_USE = 72001; // 使用道具
	public static Object CMD_GAME_BAG_ITEM_USE(RequestParam clientParam) throws Exception {
		CSGameBagItemUseRequest request = CSGameBagItemUseRequest.parseFrom(clientParam.getData());
		CSGameBagItemUseResponse.Builder response = CSGameBagItemUseResponse.newBuilder();
		long userId = clientParam.getUserId();
		int itemId = request.getItemId();
		int num = request.getNum();
		int targetId = request.getTargetId();

		if (num <= 0 || num > 999) {
			return ErrorWord.BAD_PARAM;
		}

		Item item = BagManager.getItem(itemId);
		if (itemId == 0 || item == null) {
			return ErrorWord.NOT_DATA;//
		}
		UserInfo userInfo = GameUser.getUserInfo(userId);
		long userItemNum = BagManager.getUserItemNum(userInfo, itemId);
		if (userItemNum < num) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}
		int effectType = item.getEffectType();// 加成类型
		int param1 = Tool.isEmpty(item.getParam1()) ? 0 : Integer.parseInt(item.getParam1());

		UserPower userPower = GameUser.getUserPower(userId);
		List<UserPartner> partnerList = new ArrayList<>();// 干员列表 随机要用
		UserPartner userPartner = null;// 指定干员
		UserChildCls userChildCls = null;// 指定的教室
		UserSecretary userSecretary = null; // 指定秘书
		List<UserBuild> buildList = new ArrayList<>();// 商业建筑列表 随机要用

		if (effectType == BagConstant.ITEM_EFFECT_TYPE_1) {// 随机一个性格干员,增加实力
			partnerList = PartnerManager.takeUserPartnerList(userId, userPower, param1);
			if (partnerList.size() == 0) {
				return ErrorWord.WRONG_STATUS;// 没有获得该性格的干员
			}
		} else if (effectType == BagConstant.ITEM_EFFECT_TYPE_2 || effectType == BagConstant.ITEM_EFFECT_TYPE_41) {// 指定干员,增加实力
			userPartner = PartnerManager.getUserPartner(userId, targetId);
			if (userPartner == null) {
				return ErrorWord.NOT_DATA_1;// 未获得对应的干员
			}
		} else if (effectType == BagConstant.ITEM_EFFECT_TYPE_12) {// 指定教室的体力
			userChildCls = ChildManager.getUserChildCls(userId, targetId);
			if (userChildCls == null) {
				return ErrorWord.NOT_DATA_1;// 未获得对应的教室
			}
		} else if (effectType == BagConstant.ITEM_EFFECT_TYPE_13) {// 增加商业建筑的招募次数
			for (UserBuild entity : BuildManager.getUserBuildListByType(userId, BuildConstant.BUILD_TYPE_2)) {
				if (entity.isPlace()) {
					buildList.add(entity);
				}
			}
			if (buildList.size() == 0) {
				return ErrorWord.NOT_DATA_1;// 没有获得放置商业建筑
			}
		} else if (effectType == BagConstant.ITEM_EFFECT_TYPE_3 || effectType == BagConstant.ITEM_EFFECT_TYPE_4
				|| effectType == BagConstant.ITEM_EFFECT_TYPE_42) { // 指定秘书
			userSecretary = SecretaryManager.getUserSecretary(userId, targetId);
			if (null == userSecretary) {
				return ErrorWord.NOT_DATA_1;
			}
		}

		// 执行逻辑
		PBUserData.Builder pbUserData = response.getUserBuilder();
		UserAffair userAffair = GameUser.getUserAffair(userId);
		RewardManager.subReward(userInfo, itemId, num, pbUserData, LogType.ITEM_USE);// 扣除道具
		int param2;
		switch (effectType) {
			case BagConstant.ITEM_EFFECT_TYPE_1:// 随机一个性格干员,增加实力
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				long addTotal = 0L;
				for (int i=0;i<num;i++) {
					int random = Tool.getIntRandom(partnerList.size());
					UserPartner entity = partnerList.get(random);// 随机到的干员
					addTotal += entity.updateItemPower(pbUserData, userInfo, userPower, param2);
					entity.update();
					ProtoDataUtils.updatePBUserData(pbUserData, entity);
				}
				userPower.handlePowerChange(pbUserData, userInfo, userPower.getPower() + addTotal);// 玩家的基础战力增加
				break;
			case BagConstant.ITEM_EFFECT_TYPE_2:// 指定干员,增加实力
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				long addTotal2 = userPartner.updateItemPower(pbUserData, userInfo, userPower, param2 * num);
				userPartner.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userPartner);
				userPower.handlePowerChange(pbUserData, userInfo, userPower.getPower() + addTotal2);// 玩家的基础战力增加
				break;
			case BagConstant.ITEM_EFFECT_TYPE_8:// 获得经验100003
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				RewardManager.addReward(userInfo, ItemId.PARTNER_EXP, param2 * num, pbUserData, LogType.ITEM_USE);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_9:// 获得声望100005
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				RewardManager.addReward(userInfo, ItemId.USER_REPU, param2 * num, pbUserData, LogType.ITEM_USE);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_10:// 获得金币100002
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				long earn = userPower.countCurrentEarn();// 当前 每秒的城市收益
				RewardManager.addReward(userInfo, ItemId.GOLD, param2 * earn * num, pbUserData, LogType.ITEM_USE);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_11:// 增加约会体力
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				RewardManager.addReward(userInfo, ItemId.DATING_ENERGY, param2 * num, pbUserData, LogType.ITEM_USE);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_12:// 指定教室增加体力
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				userChildCls.addEnergy(param2 * num);
				userChildCls.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userChildCls);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_13:// 增加商业建筑的招募次数
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				long addEarnTotal = 0L;// 总的增加的城市收益
				for (int i=0;i<num;i++) {
					int random = Tool.getIntRandom(buildList.size());
					UserBuild userBuild = buildList.get(random);// 随机到商业建筑
					addEarnTotal += userBuild.addRecruitNum(userInfo, userPower, param2);
					userBuild.update();
					ProtoDataUtils.updatePBUserData(pbUserData, userBuild);
				}
				userPower.addBuildEarn(pbUserData, userInfo, addEarnTotal);// 更新玩家的总城市收益
				break;
			case BagConstant.ITEM_EFFECT_TYPE_14:// 增加中餐厅的接待次数
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				UserRestaurantInfo userRestaurantInfo = RestaurantManager.getUserRestaurantInfo(userId);
				userRestaurantInfo.addReceptionNum(param2 * num);
				userRestaurantInfo.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userRestaurantInfo);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_15:// 使用后获得param2配置的道具
				List<RewardItem> rewardItemList = CommonUtils.takeReawrdItemFromStr(item.getParam2());
				CommonUtils.multipleItemList(rewardItemList, num);
				RewardManager.addReward(userInfo, rewardItemList, pbUserData, LogType.ITEM_USE);// 获得奖励
				break;
			case BagConstant.ITEM_EFFECT_TYPE_3:
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				userSecretary.addFavorite(pbUserData, userInfo, userPower, userAffair, param2 * num);
				userSecretary.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);
				MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_31, num);// 更新主线任务,成就,日常任务的进度
				MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_116, num);// 更新主线任务,成就,日常任务的进度
				RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_SECR_LIKE, param2 * num, true);
				RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_SECR_LIKE, userInfo, param2 * num, true);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_31, num);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_116, num);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_65, (long) param2 * num);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_4:
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				int addCharm = param2 * num;
				userSecretary.addCharm(pbUserData, userInfo, addCharm);
				userSecretary.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);
				MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_31, num);// 更新主线任务,成就,日常任务的进度
				MainTaskManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_116, num);// 更新主线任务,成就,日常任务的进度
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_31, num);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_116, num);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_63, addCharm);
				RankActManager.addUserrank(userInfo.getServerId(), ActTopRanking.RANK_TYPE_SECRETARY_CHARM, userInfo, addCharm, true);
				RankActManager.addUserCrossRank(userInfo, userInfo.getServerId(), ActTopRanking.RANK_TYPE_SECRETARY_CHARM, addCharm, true);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_5:
				param2 = Integer.parseInt(item.getParam2());// 增加的数值
				int addEnergy = param2 * num;
				RewardManager.addReward(userInfo, ItemId.ENERGY, addEnergy, pbUserData, LogType.ITEM_USE);
				break;
			case BagConstant.ITEM_EFFECT_TYPE_41: // 增加指定英雄(干员)实力，可开出的数值配置在param2中随机
				int totalAdd = 0;// 总的增加的战力
				for (int i=0;i<num;i++) {
					int randomNum = CommonUtils.takeParamByWeightParamStr(item.getParam2());// 随机值
					totalAdd += randomNum;
				}
				long addPower = userPartner.updateItemPower(pbUserData, userInfo, userPower, totalAdd);
				userPartner.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userPartner);
				userPower.handlePowerChange(pbUserData, userInfo, userPower.getPower() + addPower);// 玩家的基础战力增加
				break;
			case BagConstant.ITEM_EFFECT_TYPE_42: // 增加指定秘书的缘份值， 可开出的数值配置在param2中随机
				int totalFavorit = 0;// 总的增加的缘分值
				for (int i=0;i<num;i++) {
					int randomNum = CommonUtils.takeParamByWeightParamStr(item.getParam2());// 随机值
					totalFavorit += randomNum;
				}
				userSecretary.addFavorite(pbUserData, userInfo, userPower, userAffair, totalFavorit);
				userSecretary.update();
				ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);
				RankActManager.addTaskProgress(pbUserData, userInfo, MainTaskConstant.TASK_TYPE_65, totalFavorit);
				break;
		}

		return response;
	}

	// CMD_GAME_BAG_OPEN_BOX = 72002; // 开宝箱
	public static Object CMD_GAME_BAG_OPEN_BOX(RequestParam clientParam) throws Exception {
		CSGameBagOpenBoxRequest request = CSGameBagOpenBoxRequest.parseFrom(clientParam.getData());
		CSGameBagOpenBoxResponse.Builder response = CSGameBagOpenBoxResponse.newBuilder();
		long userId = clientParam.getUserId();
		int boxId = request.getBoxId();
		int totalNum = request.getTotalNum();

		if (totalNum < 0 || totalNum > 999) {
			return ErrorWord.BAD_PARAM;
		}

		Box box = BagManager.getBox(boxId);
		if (box == null) {
			return ErrorWord.NOT_DATA;
		}
		UserInfo userInfo = GameUser.getUserInfo(userId);
		long userItemNum = BagManager.getUserItemNum(userInfo, boxId);
		if (userItemNum < totalNum) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		int selectId = 0;
		if (box.getType() == BagConstant.BOX_REWARD_TYPE_2) {// 自选宝箱
			selectId = request.getBoxItemIdList().get(0);
			BoxItem boxItem = BagManager.getBoxItem(selectId);
			if (boxItem == null || boxItem.getBoxId() != boxId) {
				return ErrorWord.NOT_DATA;// 自选道具异常
			}
		}

		if (box.getType() == BagConstant.BOX_REWARD_TYPE_3) {// 随机一个和必的reward里的奖励  reward为必须配置
			if (box.getReward() == null || box.getReward().isEmpty()) {
				return ErrorWord.BAD_PARAM;
			}
		}

		// 执行逻辑
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, boxId, totalNum, pbUserData, LogType.ITEM_OPEN_BOX);//扣除宝箱
		BagManager.openBox(pbUserData, userInfo, box, totalNum, selectId, LogType.ITEM_OPEN_BOX);

		return response;
	}

	// CMD_GAME_BAG_ITEM_COMBINE = 72003; // 道具合成
	public static Object CMD_GAME_BAG_ITEM_COMBINE(RequestParam clientParam) throws Exception {
		CSGameBagItemCombineRequest request = CSGameBagItemCombineRequest.parseFrom(clientParam.getData());
		CSGameBagItemCombineResponse.Builder response = CSGameBagItemCombineResponse.newBuilder();
		long userId = clientParam.getUserId();
		int itemId = request.getItemId();
		int num = request.getNum();

		if (num < 0 || num > 999) {
			return ErrorWord.BAD_PARAM;
		}

		Item item = BagManager.getItem(itemId);
		if (item == null || !item.isComposeItem()) {
			return ErrorWord.NOT_DATA;// 不是可以合成的道具
		}

		List<RewardItem> consumeList = new ArrayList<>();// 总的需要消耗的道具
		for (RewardItem rewardItem : CommonUtils.takeReawrdItemFromStr(item.getComposeItem())) {
			long itemNum = rewardItem.getItemNum();
			rewardItem.setItemNum(itemNum * num);
			consumeList.add(rewardItem);
		}

		UserInfo userInfo = GameUser.getUserInfo(userId);
		if (!BagManager.checkNeedItemNum(userInfo, consumeList)) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		// 执行逻辑
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, consumeList, pbUserData, LogType.ITEM_COMPOSE);//扣除道具
		RewardManager.addReward(userInfo, itemId, num, pbUserData, LogType.ITEM_COMPOSE);// 获得相应奖励

		return response;
	}

	//CMD_GAME_BAG_CHIP_ONE_KEY_COMBINE = 72005; // 碎片一键合成武器，只支持武器，干员，秘书影响的数据比较，只能一个个合成
	public static Object CMD_GAME_BAG_CHIP_ONE_KEY_COMBINE(RequestParam clientParam) throws Exception {
		CSGameBagChipOneKeyCombineRequest request = CSGameBagChipOneKeyCombineRequest.parseFrom(clientParam.getData());
		CSGameBagChipOneKeyCombineResponse.Builder response = CSGameBagChipOneKeyCombineResponse.newBuilder();
		long userId = clientParam.getUserId();

		UserInfo userInfo = GameUser.getUserInfo(userId);

		List<UserChip> userChipList = BagManager.getUserChipList(userId);// 碎片列表
		if (userChipList.isEmpty()) {
			return ErrorWord.NOT_DATA;
		}


		List<RewardItem> totalRewards = new ArrayList<>();// 获得的所有武器
		List<RewardItem> consumeList = new ArrayList<>();// 扣除的所有碎片

		int currentWeaponNum = PartnerManager.countWeaponNum(userId);// 当前的武器数量
		int remainingSlots = AllParam.WEAPON_NUM_MAX - currentWeaponNum;// 剩余的数量
		if (remainingSlots > 0) {
			for (UserChip userChip : userChipList) {
				ChipMain chipMain = BagManager.getChipMain(userChip.getChipId());
				if (chipMain.getType() != BagConstant.CHIP_MAIN_TYPE_3){
					continue;
				}
				int weaponId = BagManager.takePartnerSecrIdByChip(userChip.getChipId());
				WeaponMain weaponMain = PartnerManager.getWeaponMain(weaponId);
				if (weaponMain == null) {
					continue;
				}

				int maxCombine = Math.min(remainingSlots, (int) (userChip.getNum() / chipMain.getChipNum()));// 可以合成的数量
				if (maxCombine > 0) {
					consumeList.add(new RewardItem(userChip.getChipId(), (long) chipMain.getChipNum() * maxCombine));
					totalRewards.add(new RewardItem(weaponId, maxCombine));
					remainingSlots -= maxCombine;
					if (remainingSlots <= 0){
						break; // 已经达到最大武器数量，不再继续合成
					}
				}
			}
		}

		PBUserData.Builder pbUserData = response.getUserBuilder();
		if (!consumeList.isEmpty()) { // 扣除碎片
			RewardManager.subReward(userInfo, consumeList, pbUserData, LogType.CHIP_COMPOSE);
		}

		// 添加所有奖励
		if (!totalRewards.isEmpty()) {
			RewardManager.addReward(userInfo, totalRewards, pbUserData, LogType.CHIP_COMPOSE);
		} else {
			return ErrorWord.NOT_DATA;// 没有武器合成
		}

		return response;
	}

	// CMD_GAME_BAG_CHIP_COMBINE_PARTNER = 72006; // 碎片合成 干员
	public static Object CMD_GAME_BAG_CHIP_COMBINE_PARTNER(RequestParam clientParam) throws Exception {
		CSGameBagChipCombinePartnerRequest request = CSGameBagChipCombinePartnerRequest.parseFrom(clientParam.getData());
		CSGameBagChipCombinePartnerResponse.Builder response = CSGameBagChipCombinePartnerResponse.newBuilder();

		long userId = clientParam.getUserId();
		int chipId = request.getChipId();
		int partnerId = BagManager.takePartnerSecrIdByChip(chipId);
		ChipMain chipMain = BagManager.getChipMain(chipId);
		if (chipMain == null || chipMain.getChipNum() == 0) {
			return ErrorWord.NOT_DATA;// 不是可以合成的道具
		}
		PartnerMain partnerMain = PartnerManager.getPartnerMain(partnerId);
		if (partnerMain == null) {
			return ErrorWord.NOT_DATA;
		}

		UserPartner userPartner = PartnerManager.getUserPartner(userId, partnerId);
		if (userPartner != null) {
			return ErrorWord.REPEATED_OPERATION;// 干员不能重复合成
		}

		int subNum = chipMain.getChipNum();// 扣除碎片的数量
		UserChip userChip = BagManager.getUserChip(userId, chipId);
		if (userChip == null || userChip.getNum() < subNum) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		// 执行逻辑
		UserInfo userInfo = GameUser.getUserInfo(userId);
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, chipId, subNum, pbUserData, LogType.CHIP_COMPOSE);
		RewardManager.addReward(userInfo, partnerId, 1, pbUserData, LogType.CHIP_COMPOSE);

		return response;
	}

	// CMD_GAME_BAG_CHIP_COMBINE_SECRETARY = 72007; // 碎片合成 秘书
	public static Object CMD_GAME_BAG_CHIP_COMBINE_SECRETARY(RequestParam clientParam) throws Exception {
		CSGameBagChipCombineSecretaryRequest request = CSGameBagChipCombineSecretaryRequest.parseFrom(clientParam.getData());
		CSGameBagChipCombineSecretaryResponse.Builder response = CSGameBagChipCombineSecretaryResponse.newBuilder();

		long userId = clientParam.getUserId();
		int chipId = request.getChipId();
		int secrId = BagManager.takePartnerSecrIdByChip(chipId);
		ChipMain chipMain = BagManager.getChipMain(chipId);
		if (chipMain == null || chipMain.getChipNum() == 0) {
			return ErrorWord.NOT_DATA;// 不是可以合成的道具
		}
		SecretaryMain secretaryMain = SecretaryManager.getSecretaryMain(secrId);
		if (secretaryMain == null) {
			return ErrorWord.NOT_DATA;
		}

		UserSecretary userSecretary = SecretaryManager.getUserSecretary(userId, secrId);
		if (userSecretary != null) {
			return ErrorWord.REPEATED_OPERATION;// 干员不能重复合成
		}

		int subNum = chipMain.getChipNum();// 扣除碎片的数量
		UserChip userChip = BagManager.getUserChip(userId, chipId);
		if (userChip == null || userChip.getNum() < subNum) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		// 执行逻辑
		UserInfo userInfo = GameUser.getUserInfo(userId);
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, chipId, subNum, pbUserData, LogType.CHIP_COMPOSE);
		RewardManager.addReward(userInfo, secrId, 1, pbUserData, LogType.CHIP_COMPOSE);

		return response;
	}

	// CMD_GAME_BAG_CHIP_USE = 72008; // 使用秘书碎片，增加秘书的缘分值
	public static Object CMD_GAME_BAG_CHIP_USE(RequestParam clientParam) throws Exception {
		CSGameBagChipUseRequest request = CSGameBagChipUseRequest.parseFrom(clientParam.getData());
		CSGameBagChipUseResponse.Builder response = CSGameBagChipUseResponse.newBuilder();

		long userId = clientParam.getUserId();
		int chipId = request.getChipId();
		int subNum = request.getNum();

		if (subNum < 1 || subNum > 999) {
			return ErrorWord.BAD_PARAM;
		}

		ChipMain chipMain = BagManager.getChipMain(chipId);
		int addValue = 0;// 增加的缘分值数量
		try {
			addValue = Integer.parseInt(chipMain.getParam());
		} catch (Exception e) {}

		if (chipMain == null || addValue == 0) {
			return ErrorWord.NOT_DATA;// 增加的缘分值数量=0
		}

		int secrId = BagManager.takePartnerSecrIdByChip(chipId);
		SecretaryMain secretaryMain = SecretaryManager.getSecretaryMain(secrId);
		if (secretaryMain == null) {
			return ErrorWord.NOT_DATA;
		}

		UserSecretary userSecretary = SecretaryManager.getUserSecretary(userId, secrId);
		if (userSecretary == null) {
			return ErrorWord.WRONG_STATUS;// 尚未获得秘书
		}

		UserChip userChip = BagManager.getUserChip(userId, chipId);
		if (userChip == null || userChip.getNum() < subNum) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		// 执行逻辑
		UserInfo userInfo = GameUser.getUserInfo(userId);
		UserPower userPower = GameUser.getUserPower(userId);
		UserAffair userAffair = GameUser.getUserAffair(userId);
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, chipId, subNum, pbUserData, LogType.CHIP_USE);
		userSecretary.addFavorite(pbUserData, userInfo, userPower, userAffair, addValue * subNum); // 增加缘分值
		userSecretary.update();
		ProtoDataUtils.updatePBUserData(pbUserData, userSecretary);

		return response;
	}


	// CMD_GAME_BAG_CHIP_COMBINE_WEAPON = 72009; // 碎片合成 武器
	public static Object CMD_GAME_BAG_CHIP_COMBINE_WEAPON(RequestParam clientParam) throws Exception {
		CSGameBagChipCombineWeaponRequest request = CSGameBagChipCombineWeaponRequest.parseFrom(clientParam.getData());
		CSGameBagChipCombineWeaponResponse.Builder response = CSGameBagChipCombineWeaponResponse.newBuilder();

		long userId = clientParam.getUserId();
		int chipId = request.getChipId();
		int num = request.getNum();
		if (num < 0 || num > 999) {
			return ErrorWord.BAD_PARAM;
		}
		int weaponId = BagManager.takePartnerSecrIdByChip(chipId);
		ChipMain chipMain = BagManager.getChipMain(chipId);
		if (chipMain == null || chipMain.getChipNum() == 0) {
			return ErrorWord.NOT_DATA;// 不是可以合成的道具
		}
		WeaponMain weaponMain = PartnerManager.getWeaponMain(weaponId);
		if (weaponMain == null) {
			return ErrorWord.NOT_DATA;
		}

		UserChip userChip = BagManager.getUserChip(userId, chipId);
		if (userChip == null || userChip.getNum() < num || num < chipMain.getChipNum()) {
			return ErrorWord.ITEM_LACK;// 数量不足
		}

		int currentWeaponNum = PartnerManager.countWeaponNum(userId);// 当前的武器数量
		int remainingSlots = AllParam.WEAPON_NUM_MAX - currentWeaponNum;// 剩余的数量
		if (remainingSlots < 0) {
			return ErrorWord.ALREADY_MAX;// 武器数量达到上限了
		}

		int maxCombine = Math.min(remainingSlots, (int) (num / chipMain.getChipNum()));// 可以合成的数量
		int subNum = chipMain.getChipNum() * maxCombine;// 扣除碎片的数量

		// 执行逻辑
		UserInfo userInfo = GameUser.getUserInfo(userId);
		PBUserData.Builder pbUserData = response.getUserBuilder();
		RewardManager.subReward(userInfo, chipId, subNum, pbUserData, LogType.CHIP_COMPOSE);
		RewardManager.addReward(userInfo, weaponId, maxCombine, pbUserData, LogType.CHIP_COMPOSE);

		return response;
	}
}
